package com;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ami.weather.R;
import com.jy.common.Tools;
import com.jy.unkown.AD.UnkownInterstitalAd;
import com.jy.unkown.AD.UnkwonReward;
import com.jy.unkown.AdReqCallBack;
import com.jy.unkown.inter.IInterstitialCallBack;
import com.jy.unkown.inter.IRewardCallBack;
import com.jy.unkown.page.AskDialog;
import com.jy.utils.call.NoDoubleClick;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public class RewardActivity extends BBActivity {
    static final String TAG = "Reward";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_reward);

        findViewById(R.id.jiazai).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                loadAd();
//
//                AskDialog askDialog = new AskDialog(RewardActivity.this);
//                String tips = "再看1s后可领取奖励";
//                askDialog.setTips(tips);
//                askDialog.setDialogInterface(new AskDialog.DialogInterface() {
//                    @Override
//                    public void dismiss() {
//
//                    }
//
//                    @Override
//                    public void exit() {
//
//                    }
//                });
//
//
//                Tools.showCommonDialog(RewardActivity.this,
//                        askDialog, true);



            }
        });
        findViewById(R.id.zhanshi).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                zhanshi();
            }
        });
    }


    UnkwonReward unkwonReward;

    IRewardCallBack adCallBack = new IRewardCallBack() {
        @Override
        public void skip() {
            Log.e(TAG, "跳过");
        }

        @Override
        public void reward() {
            Log.e(TAG, "奖励");
        }

        @Override
        public void close() {
            Log.e(TAG, "关闭");
        }

        @Override
        public void click() {
            Log.e(TAG, "点击");
        }

        @Override
        public void exposure() {
            Log.e(TAG, "曝光成功");
        }

        @Override
        public void err() {
            Log.e(TAG, "曝光失败");
        }
    };


    AtomicBoolean isRequesting = new AtomicBoolean(false);
    AtomicBoolean isRequestSucc = new AtomicBoolean(false);

    AdReqCallBack adReqCallBack = new AdReqCallBack() {
        @Override
        public void reqSucc() {
            Log.e(TAG, "拉取广告成功");
            showToast("拉取广告成功");
            isRequestSucc.set(true);
            isRequesting.set(false);
        }

        @Override
        public void reqErr() {
            showToast("拉取广告失败");
            Log.e(TAG, "拉取广告失败");
            isRequesting.set(false);
        }
    };


    protected void loadAd() {
        if (isRequesting.get()) {
            return;
        }
        if (unkwonReward == null) {
            unkwonReward = new UnkwonReward();
        }
        unkwonReward.setAdCallBack(adCallBack);
        unkwonReward.setRequestId(UUID.randomUUID().toString());
        unkwonReward.setAdReqCallBack(adReqCallBack);
        unkwonReward.setAppId(Key.APPID);
        unkwonReward.setAdId(Key.REWARD_ID);
        unkwonReward.setVideoMute(true);
        unkwonReward.setPrice(0);
        isRequesting.set(true);
        isRequestSucc.set(false);
        unkwonReward.loadAd();
    }


    protected void zhanshi() {
        if (!isRequestSucc.get()) {
            showToast("广告未请求成功");
            return;
        }
        unkwonReward.showAD(this);
    }
}
